package net.cn_su.sharpeyes.update;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.support.v7.app.AlertDialog;

import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;

import net.cn_su.sharpeyes.utils.http.HttpClient;
import net.cn_su.sharpeyes.utils.http.HttpUrl;
import net.cn_su.sharpeyes.utils.http.JacksonMapper;

import cz.msebera.android.httpclient.Header;

/**
 * 检测更新的工具类
 */
public class AppUpdateUtil {


    private Context mContext;
    private RequestParams params;

    public AppUpdateUtil(Activity context, RequestParams params) {

        this.mContext = context;
        this.params = params;
    }


    public void checkUpdate(final UpdateCallBack callBack) {


        HttpClient.get(HttpUrl.UPDATE, params, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                //Logger.v("onFailure" + statusCode);
                callBack.onError();
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {

                try {

                    final Version version = JacksonMapper.parse(responseString, Version.class);

               //     Logger.v("url==" + version.getFile_url());

                    if (Float.parseFloat(version.getKey_code()) > getVersionCode(mContext)) {
                        callBack.isUpdate(responseString);

                        final AlertDialog materialDialog = new AlertDialog.Builder(mContext)
                                .setMessage(version.getMessage())
                                .setTitle("更新提示")
                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        Intent intent = new Intent(mContext, DownLoadService.class);
                                        String file_url = HttpUrl.UPDATE_ROOT + version.getFile_url();
                                        intent.putExtra("url", file_url);
                                        mContext.startService(intent);
                                        dialog.dismiss();
                                    }
                                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                    }
                                }).create();

                        materialDialog.setCanceledOnTouchOutside(false);
                        materialDialog.show();


                    } else {

                        callBack.isNoUpdate();
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }


        });


    }

    /**
     * [获取应用程序版本名称信息]
     *
     * @param context
     * @return 当前应用的版本
     */
    public int getVersionCode(Context context) {
        try {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(
                    context.getPackageName(),
                    PackageManager.GET_CONFIGURATIONS);
            return packageInfo.versionCode;

        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
